package com.cskaoyan.market.service.admin;

import com.cskaoyan.market.db.domain.MarketRole;
import com.cskaoyan.market.db.domain.MarketRoleExample;
import com.cskaoyan.market.db.mapper.MarketRoleMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: chenjian
 * @Date: 2024/5/22 10:54
 */
@Service
public class MarketRoleServiceImpl implements MarketRoleService {
    @Autowired
    MarketRoleMapper marketRoleMapper;

    @Override
    public List<MarketRole> list(Integer pageInt, Integer limitInt, String sort, String order, String name) {


        MarketRoleExample marketRoleExample = new MarketRoleExample();
        marketRoleExample.setOrderByClause(sort + " " + order);
        MarketRoleExample.Criteria criteria = marketRoleExample.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        PageHelper.startPage(pageInt, limitInt);
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(marketRoleExample);

        System.out.println("marketRolesSize = " + marketRoles.size());
        return marketRoles;
    }

    @Override
    public MarketRole update(MarketRole marketRole) {
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = marketRoleExample.createCriteria();
        criteria.andIdEqualTo(marketRole.getId());
        int i = marketRoleMapper.updateByExample(marketRole, marketRoleExample);
        if (i != 0)
            return marketRole;
        else return null;
    }

    @Override
    public MarketRole delete(MarketRole marketRole) {
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = marketRoleExample.createCriteria();
        criteria.andIdEqualTo(marketRole.getId());
        int i = marketRoleMapper.deleteByExample(marketRoleExample);
        if (i != 0)
            return marketRole;
        else return null;
    }
}
